package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.colorpicker.lib.ColorPicker;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityDrawBinding;
import flc.ast.databinding.ActivityDrawTempBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class DrawActivity extends BaseAc<ActivityDrawBinding> {

    public static int kind = 0;//0模板 1空画板
    public static int selImg;//模板
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private ColorAdapter brushAdapter;
    private int oldBrushPos = 1;
    private ColorAdapter bgAdapter;
    private int oldBgPos = 1;
    private ColorAdapter textAdapter;
    private int oldTextPos = 1;
    private ColorPicker mColorPicker;
    private int style = 0;
    private int bgColor, brushColor, textColor;

    @Override
    protected int onCreate() {
        return R.layout.activity_draw;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivDrawBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDrawSave.setOnClickListener(this);
        mDataBinding.ivBackground.setOnClickListener(this);
        mDataBinding.ivBrush.setOnClickListener(this);
        mDataBinding.ivEraser.setOnClickListener(this);
        mDataBinding.ivText.setOnClickListener(this);

        //背景颜色
        mDataBinding.rvBackgroundColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        bgAdapter = new ColorAdapter();
        mDataBinding.rvBackgroundColorList.setAdapter(bgAdapter);
        bgAdapter.setOnItemClickListener(this);
        //画笔颜色
        mDataBinding.rvBrushColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        brushAdapter = new ColorAdapter();
        mDataBinding.rvBrushColorList.setAdapter(brushAdapter);
        brushAdapter.setOnItemClickListener(this);
        //文字颜色
        mDataBinding.rvTextColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        textAdapter = new ColorAdapter();
        mDataBinding.rvTextColorList.setAdapter(textAdapter);
        textAdapter.setOnItemClickListener(this);
        //颜色选择器
        showColorPicker();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDrawSave://保存
                saveImg();
                break;
            case R.id.ivBackground://背景
                clearView();
                mDataBinding.ivBackground.setImageResource(R.drawable.bjdk);
                mDataBinding.llBackground.setVisibility(View.VISIBLE);
                break;
            case R.id.ivBrush://画笔
                clearView();
                mDataBinding.ivBrush.setImageResource(R.drawable.hbdk);
                mDataBinding.llBrush.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(false);
                penBrush.setSize(paintWidth);
                break;
            case R.id.ivEraser://橡皮擦
                clearView();
                mDataBinding.ivEraser.setImageResource(R.drawable.xpcdk);
                mDataBinding.llEraser.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(true);
                penBrush.setSize(eraserWidth);
                break;
            case R.id.ivText://文字
                clearView();
                mDataBinding.ivText.setImageResource(R.drawable.wzdk);
                mDataBinding.llText.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == brushAdapter) {
            if (position == 0) {
                style = 1;
                mColorPicker.show();
            } else {
                brushAdapter.getItem(oldBrushPos).setSelect(false);
                brushAdapter.notifyItemChanged(oldBrushPos);
                oldBrushPos = position;
                brushAdapter.getItem(position).setSelect(true);
                brushAdapter.notifyItemChanged(position);
                brushColor = Color.parseColor(brushAdapter.getItem(position).getColor());
                penBrush.setColor(Color.parseColor(brushAdapter.getItem(position).getColor()));
            }
        } else if (adapter == textAdapter) {
            if (position == 0) {
                style = 2;
                mColorPicker.show();
            } else {
                textAdapter.getItem(oldTextPos).setSelect(false);
                textAdapter.notifyItemChanged(oldTextPos);
                oldTextPos = position;
                textAdapter.getItem(position).setSelect(true);
                textAdapter.notifyItemChanged(position);
                textColor = Color.parseColor(textAdapter.getItem(position).getColor());
                mDataBinding.txtStickerView.setTextColor(Color.parseColor(textAdapter.getItem(position).getColor()));
            }
        } else if (adapter == bgAdapter) {
            if (position == 0) {
                style = 3;
                mColorPicker.show();
            } else {
                bgAdapter.getItem(oldBgPos).setSelect(false);
                bgAdapter.notifyItemChanged(oldBgPos);
                oldBgPos = position;
                bgAdapter.getItem(position).setSelect(true);
                bgAdapter.notifyItemChanged(position);
                bgColor = Color.parseColor(bgAdapter.getItem(position).getColor());
                mDataBinding.rlDrawBgView.setBackgroundColor(Color.parseColor(bgAdapter.getItem(position).getColor()));
            }
        }
    }

    @Override
    protected void initData() {
        if (kind == 0) {
            Glide.with(mContext).load(selImg).into(mDataBinding.ivDrawImg);
        }
        initPen();
        //画笔/文字颜色
        List<MyColorBean> listColor1 = new ArrayList<>();
        listColor1.add(new MyColorBean("", false));
        listColor1.add(new MyColorBean("#333333", true));
        listColor1.add(new MyColorBean("#FFFFFF", false));
        listColor1.add(new MyColorBean("#F44236", false));
        listColor1.add(new MyColorBean("#FB6400", false));
        listColor1.add(new MyColorBean("#F7B501", false));
        listColor1.add(new MyColorBean("#6ED400", false));
        listColor1.add(new MyColorBean("#44D7B5", false));
        listColor1.add(new MyColorBean("#54B7F8", false));
        listColor1.add(new MyColorBean("#4DD0E2", false));
        listColor1.add(new MyColorBean("#4CB6AC", false));
        listColor1.add(new MyColorBean("#80C783", false));
        listColor1.add(new MyColorBean("#AED582", false));
        listColor1.add(new MyColorBean("#DDE775", false));
        listColor1.add(new MyColorBean("#FFF176", false));
        brushAdapter.setList(listColor1);
        List<MyColorBean> listColor2 = new ArrayList<>();
        listColor2.add(new MyColorBean("", false));
        listColor2.add(new MyColorBean("#333333", true));
        listColor1.add(new MyColorBean("#FFFFFF", false));
        listColor2.add(new MyColorBean("#F44236", false));
        listColor2.add(new MyColorBean("#FB6400", false));
        listColor2.add(new MyColorBean("#F7B501", false));
        listColor2.add(new MyColorBean("#6ED400", false));
        listColor2.add(new MyColorBean("#44D7B5", false));
        listColor2.add(new MyColorBean("#54B7F8", false));
        listColor2.add(new MyColorBean("#4DD0E2", false));
        listColor2.add(new MyColorBean("#4CB6AC", false));
        listColor2.add(new MyColorBean("#80C783", false));
        listColor2.add(new MyColorBean("#AED582", false));
        listColor2.add(new MyColorBean("#DDE775", false));
        listColor2.add(new MyColorBean("#FFF176", false));
        textAdapter.setList(listColor2);
        //背景颜色
        List<MyColorBean> listBg = new ArrayList<>();
        listBg.add(new MyColorBean("", false));
        listBg.add(new MyColorBean("#FFFFFF", true));
        listBg.add(new MyColorBean("#FFCDD2", false));
        listBg.add(new MyColorBean("#E1BEE8", false));
        listBg.add(new MyColorBean("#C4C9E9", false));
        listBg.add(new MyColorBean("#B9DAF9", false));
        listBg.add(new MyColorBean("#B3E5FC", false));
        listBg.add(new MyColorBean("#B2EBF2", false));
        listBg.add(new MyColorBean("#B2DFDC", false));
        listBg.add(new MyColorBean("#C8E6CA", false));
        listBg.add(new MyColorBean("#DDEDC8", false));
        listBg.add(new MyColorBean("#FCEFBA", false));
        listBg.add(new MyColorBean("#FFE0B2", false));
        listBg.add(new MyColorBean("#E0E0E0", false));
        bgAdapter.setList(listBg);
        //文字输入监听
        mDataBinding.etDrawText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = mDataBinding.etDrawText.getText().toString();
                mDataBinding.txtStickerView.setText(text);
            }
        });
        //默认文字黑色
        mDataBinding.txtStickerView.setTextColor(Color.parseColor("#333333"));

        //背景透明度
        mDataBinding.sbBackgroundAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int alpha = (int) (255 * ((float) progress / 100)); // 50%透明度，255是完全不透明
                int color = Color.argb(alpha, Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
                mDataBinding.rlDrawBgView.setBackgroundColor(color);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //画笔透明度
        mDataBinding.sbBrushAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int alpha = (int) (255 * ((float) progress / 100)); // 50%透明度，255是完全不透明
                int color = Color.argb(alpha, Color.red(brushColor), Color.green(brushColor), Color.blue(brushColor));
                penBrush.setColor(color);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //文字透明度
        mDataBinding.sbTextAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int alpha = (int) (255 * ((float) progress / 100)); // 50%透明度，255是完全不透明
                int color = Color.argb(alpha, Color.red(textColor), Color.green(textColor), Color.blue(textColor));
                mDataBinding.txtStickerView.setTextColor(color);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#333333"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小&橡皮擦大小
        mDataBinding.sbBrushSize.setMax(50);
        mDataBinding.sbBrushSize.setProgress(5);
        mDataBinding.sbBrushSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mDataBinding.sbEraserSize.setMax(50);
        mDataBinding.sbEraserSize.setProgress(5);
        mDataBinding.sbEraserSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //橡皮擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //显示颜色选择器
    private void showColorPicker() {
        mColorPicker = new ColorPicker(this);
        mColorPicker.setListener(pickedColor -> {
            switch (style) {
                case 1://画笔颜色
                    brushAdapter.getItem(oldBrushPos).setSelect(false);
                    brushAdapter.notifyItemChanged(oldBrushPos);
                    oldBrushPos = 0;
                    brushAdapter.getItem(0).setSelect(true);
                    brushAdapter.notifyItemChanged(0);
                    brushColor = Color.parseColor(ColorUtils.int2RgbString(pickedColor));
                    penBrush.setColor(Color.parseColor(ColorUtils.int2RgbString(pickedColor)));
                    break;
                case 2://文字颜色
                    textAdapter.getItem(oldTextPos).setSelect(false);
                    textAdapter.notifyItemChanged(oldTextPos);
                    oldTextPos = 0;
                    textAdapter.getItem(0).setSelect(true);
                    textAdapter.notifyItemChanged(0);
                    textColor = Color.parseColor(ColorUtils.int2RgbString(pickedColor));
                    mDataBinding.txtStickerView.setTextColor(Color.parseColor(ColorUtils.int2RgbString(pickedColor)));
                    break;
                case 3://背景颜色
                    bgAdapter.getItem(oldBgPos).setSelect(false);
                    bgAdapter.notifyItemChanged(oldBgPos);
                    oldBgPos = 0;
                    bgAdapter.getItem(0).setSelect(true);
                    bgAdapter.notifyItemChanged(0);
                    mDataBinding.rlDrawBgView.setBackgroundColor(Color.parseColor(ColorUtils.int2RgbString(pickedColor)));
                    break;
            }
        });
    }

    //保存到首页记录
    private void saveImg() {
        showDialog(getString(R.string.saving));
        mDataBinding.txtStickerView.setShowHelpBox(false);
        mDataBinding.ivDrawImg.setVisibility(View.GONE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<Bitmap>() {

                    @Override
                    public void doBackground(ObservableEmitter<Bitmap> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlDrawBgView);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext(bitmap);
                    }

                    @Override
                    public void accept(Bitmap bitmap) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_success);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 500);
    }

    //管理画笔样式
    private void clearView() {
        mDataBinding.ivBackground.setImageResource(R.drawable.bjgb);
        mDataBinding.ivBrush.setImageResource(R.drawable.hbgb);
        mDataBinding.ivEraser.setImageResource(R.drawable.xpcgb);
        mDataBinding.ivText.setImageResource(R.drawable.wzgb);
        mDataBinding.llBackground.setVisibility(View.GONE);
        mDataBinding.llBrush.setVisibility(View.GONE);
        mDataBinding.llEraser.setVisibility(View.GONE);
        mDataBinding.llText.setVisibility(View.GONE);
    }


}